package com.bolang.onlycode.demo.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class test {
    public static void main(String[] args) throws IOException {
//        XWPFTemplate template = XWPFTemplate.compile("src/main/resources/template.docx").render(
//                new HashMap<String, Object>(){{
//                    put("title", "Hi, poi-tl Word模板引擎");
//                }});
//        template.writeAndClose(new FileOutputStream("output.docx"));

        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        Configure config = Configure.builder()
                .bind("drugs", policy).build();
        List<Drug> goods = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Drug good = new Drug();
            good.setDrugName("药品111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+i);
            good.setCount(i);
            good.setRouteName("口服"+i);
            goods.add(good);
        }
        XWPFTemplate template = XWPFTemplate.compile("src/main/resources/template.docx", config).render(
                new HashMap<String, Object>() {{
                    put("drugs", goods);
                    put("dept", "张三111111111111111111111111111111111111111111111111111111111111111111");
                    put("sex", "男");
                    put("approvalTime3", "2022-10-11 12:00:00");
                }}
        );
        PdfOptions options = PdfOptions.create().fontProvider(FontRegistry.get("SIMSUN"));
        try (ByteArrayOutputStream outPDF =new ByteArrayOutputStream()) {
            PdfConverter.getInstance().convert(template.getXWPFDocument(), outPDF, options);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outPDF.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        template.writeAndClose(new FileOutputStream("output.docx"));


    }

}
